package org.example.struct.array;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/12/5 10:48
 * @apiNote
 */

public class Test00350Intersect {

    public static int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer,Integer> map1 = new HashMap<>();
        Map<Integer,Integer> map2 = new HashMap<>();
        List<Integer> result = new ArrayList<>();
        for (int num : nums1) {
            map1.put(num,map1.getOrDefault(num,0)+1);
        }
        for (int num : nums2) {
            map2.put(num,map2.getOrDefault(num,0)+1);
        }
        for (Integer key : map1.keySet()) {
            if(map2.containsKey(key)){
                int count = Math.min(map1.get(key),map2.get(key));
                for (int i = 0; i < count; i++) {
                    result.add(key);
                }
            }
        }
        int[] resArr = new int[result.size()];
        for(int j = 0; j < result.size(); j++){
            resArr[j] = result.get(j);
        }
        return resArr;
    }
}
